package com.ccjq.jq173.member;

import com.ccjq.jq173.member.po.User;
import com.ccjq.jq173.member.service.UserService;
import com.ccjq.jq173.member.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class UserServlet extends HttpServlet {
    private static final String FLAG_L="login";
    private static final String FLAG_R="regist";
    private static final String FLAG_Q="finduser";
    private static final String FLAG_O="logout";

    private static final String FLAG_L_PAGE="lp";//跳转登陆
    private static final String FLAG_R_PAGE="rp";//跳转注册页面


    private UserService userService;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String flag=request.getParameter("flag");
        if (FLAG_L.equals(flag))
        {
            //登陆
            login(request,response);
        }
        else if(FLAG_R.equals(flag))
        {
            //添加
            regist(request,response);
        }
        else if(FLAG_Q.equals(flag))
        {
            //获取用户信息
            finduser(request,response);
        }
        //跳转登陆
        else if(FLAG_L_PAGE.equals(flag))
        {
            toLoginPage(request,response);
        }
        //跳转注册
        else if(FLAG_R_PAGE.equals(flag))
        {
            toRegistPage(request,response);
        }
        else if(FLAG_O.equals(flag))
        {
            logout(request,response);
        }
    }
    //注销
    private void logout(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
    {
        HttpSession session=request.getSession();
        Object obj=session.getAttribute("user");
        if(obj!=null)
        {
            session.setAttribute("user",null);
        }
        request.getRequestDispatcher("/WEB-INF/jsp/member/login.jsp").forward(request,response);
    }
    //跳转登陆
    private void toLoginPage(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
    {
        request.getRequestDispatcher("/WEB-INF/jsp/member/login.jsp").forward(request,response);
    }
    //跳转注册
    private void toRegistPage(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
    {
        request.getRequestDispatcher("/WEB-INF/jsp/member/regist.jsp").forward(request,response);
    }
    //登陆功能
    private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    { userService=new UserServiceImpl();
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String code=request.getParameter("vicode");
        String checkcode=(String)request.getSession().getAttribute("checkcode");
        if(code.equals(checkcode))
        {
            User u=userService.login(username,password);
            if(u!=null)
            {
                request.getSession().setAttribute("user",u);
                request.getRequestDispatcher("/home").forward(request,response);
            }else
            {
                request.setAttribute("errMsg","用户不存在");
                request.getRequestDispatcher("/WEB-INF/jsp/member/login.jsp").forward(request,response);
            }
        }
        else
        {
            request.setAttribute("errMsg","验证码错误");
            request.getRequestDispatcher("/WEB-INF/jsp/member/login.jsp").forward(request,response);
        }
    }
    //注册功能
    private void regist(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
    {userService=new UserServiceImpl();
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String code=request.getParameter("vicode");
        String phone=request.getParameter("phone");
        String checkcode=(String)request.getSession().getAttribute("checkcode");
        if(code.equals(checkcode))
        {
            int result=userService.regist(username,password,phone);
            if(result==1)
            {
                User user=userService.findUser(username);
                request.setAttribute("user",user);
                request.getRequestDispatcher("/WEB-INF/jsp/member/login.jsp").forward(request,response);

            }
            else
            {
                request.setAttribute("errMsg","注册失败");
                request.getRequestDispatcher("/WEB-INF/jsp/member/regist.jsp").forward(request,response);
            }
        }
        else
        {
            request.setAttribute("errMsg","验证码错误");
            request.getRequestDispatcher("/WEB-INF/jsp/member/regist.jsp").forward(request,response);
        }
    }
    //个人信息
    private void finduser(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
    {
        userService=new UserServiceImpl();
        String username=request.getParameter("username");
        User user=userService.findUser(username);
        request.setAttribute("user",user);
        request.getRequestDispatcher("/WEB-INF/jsp/member/userinfo.jsp").forward(request,response);
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
